package com.lysky.selfllm;

import dev.langchain4j.community.model.zhipu.ZhipuAiChatModel;
import dev.langchain4j.community.model.zhipu.ZhipuAiImageModel;
import dev.langchain4j.community.model.zhipu.ZhipuAiStreamingChatModel;
import dev.langchain4j.data.image.Image;
import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.chat.StreamingChatLanguageModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import dev.langchain4j.model.chat.response.StreamingChatResponseHandler;
import dev.langchain4j.model.image.ImageModel;
import dev.langchain4j.model.output.Response;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.time.Duration;
import java.util.Collections;
import java.util.concurrent.TimeUnit;

//@SuppressWarnings("all")
@SpringBootApplication
public class SelfLlmApplication {

    protected final static String apiKey = "6314c213f32e47909374d0bd1d8f21b7.dDxdwvalRC431kgz";

    public static void main(String[] args) {
        SpringApplication.run(SelfLlmApplication.class, args);
//        demo1();
    }



    protected static void demo1(){
        ChatLanguageModel  model = ZhipuAiChatModel.builder()
                .apiKey(apiKey)
                .model("glm-4-flash")
                .temperature(0.6)
                .maxToken(1024)
                .maxRetries(1)
                .callTimeout(Duration.ofSeconds(60))
                .connectTimeout(Duration.ofSeconds(60))
                .writeTimeout(Duration.ofSeconds(60))
                .readTimeout(Duration.ofSeconds(60))
                .build();
        UserMessage um = UserMessage.from("您是谁");
        ChatResponse response = model.chat(um);
        AiMessage aiMessage = response.aiMessage();
        System.err.println(aiMessage.text());

        UserMessage um2 = UserMessage.from("请您重复一次上面说的内容");
        ChatResponse response2 = model.chat(um, aiMessage, um2);
        System.err.println(response2.aiMessage().text());
    }

    protected static void demo2() {
        StreamingChatLanguageModel model = ZhipuAiStreamingChatModel.builder()
                .apiKey(apiKey)
                .model("glm-4-flash")
                .temperature(0.6)
                .maxToken(1024)
                .callTimeout(Duration.ofSeconds(60))
                .connectTimeout(Duration.ofSeconds(60))
                .writeTimeout(Duration.ofSeconds(60))
                .readTimeout(Duration.ofSeconds(60))
                .build();
        UserMessage um = UserMessage.from("您是谁");
        model.chat(Collections.singletonList(um), new StreamingChatResponseHandler() {
            @Override
            public void onPartialResponse(String partialResponse) {
                System.err.println(partialResponse);
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }

            @Override
            public void onCompleteResponse(ChatResponse completeResponse) {
                System.err.println("我回答完毕");
            }

            @Override
            public void onError(Throwable error) {

            }
        });
    }

    protected static void demo3() {
        ImageModel model = ZhipuAiImageModel.builder()
                .apiKey(apiKey)
                .model("cogview-3-flash")
                .maxRetries(1)
                .callTimeout(Duration.ofSeconds(60))
                .connectTimeout(Duration.ofSeconds(60))
                .writeTimeout(Duration.ofSeconds(60))
                .readTimeout(Duration.ofSeconds(60))
                .build();
        Response<Image> response = model.generate("请您生成一张赛博朋克风格的电脑图片");
        System.err.println(response.content().url());
    }
}
